//
//  HMPatient.swift
//  HealthMonitoring
//
//  Created by YTYanK on 24/01/2019.
//  Copyright © 2019 YTYanK. All rights reserved.
//

import UIKit
import SwiftyJSON

class HMPatient: HMViewController {
    
    var dataJson = JSON()
    
    
    
    let head = UIView()
    let headIcon = UIImageView()
    let name = UILabel()
    let headDes = UILabel()
    
    let lifeState = UILabel()
    let lifePattern = UILabel()
    let lifeValue = UILabel()
    let motionPattern = UILabel()
    let motionValue = UILabel()
    
    let diseaseTypes = UILabel()
    let disease = UILabel() //疾病
    let describeCondition = UILabel() // 病情描述
    let describe = UILabel()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
//        print("=====》〉", self.dataJson)
        self.setJsonData(json: self.dataJson)
    }
    
 
//    override func viewDidDisappear(_ animated: Bool) {
//        super.viewDidDisappear(animated)
//
//
//    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.navigationController?.navigationBar.tintColor =  configure.shareConfigure.HMColor1
        self.navigationController?.navigationBar.barTintColor = UIColor.white
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
       
    }
    
    override func setupView() {
        self.title = ""
        
        self.navigationController?.navigationBar.tintColor =  UIColor.white
        self.navigationController?.navigationBar.barTintColor = configure.shareConfigure.HMColor1
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
        head.addSubview(headIcon)
        head.addSubview(name)
        head.addSubview(headDes)
        view.addSubview(head)
        view.addSubview(lifeState)
        view.addSubview(lifePattern)
        view.addSubview(lifeValue)
        view.addSubview(motionPattern)
        view.addSubview(motionValue)
        view.addSubview(diseaseTypes)
        view.addSubview(disease)
        view.addSubview(describeCondition)
        view.addSubview(describe)
        
        headIcon.image = UIImage.init(named: "pl")
        headIcon.leadingAlign(view: head, predicate: "8")
        headIcon.centerYAlign(view: head, predicate: "0")
        headIcon.heightConstrain(predicate: "75")
        headIcon.widthConstrain(predicate: "75")
        headIcon.layer.cornerRadius = 37.5
        headIcon.layer.masksToBounds = true
        
        name.topAlign(view: headIcon, predicate: "0")
        name.leadingHorizontally(view: headIcon, predicate: "20")
        name.heightConstrain(predicate: "30")
        name.textColor = UIColor.white
//        name.text = "XXXXXXX"
        
        headDes.topVertically(view: name, predicate: "10")
        headDes.leadingHorizontally(view: headIcon, predicate: "20")
        headDes.heightConstrain(predicate: "30")
        headDes.textColor = UIColor.white
//        headDes.text = "XXXXXX      XXXXX    XXX  "
        

        view.backgroundColor = UIColor.white
        head.backgroundColor = configure.shareConfigure.HMColor1
        head.topAlign(view: view, predicate: "0")
        head.leadingAlign(view: view, predicate: "0")
        head.trailingAlign(view: view, predicate: "0")
        head.height(view: view, predicate: "*0.15")
       
        
        lifeState.topVertically(view: head, predicate: "10")
        lifeState.leadingAlign(view: view, predicate: "10")
        lifeState.trailingAlign(view: view, predicate: "0")
        lifeState.heightConstrain(predicate: "44")
        lifeState.text = "生活状态"
        let color = configure.shareConfigure.HMColor3
        lifeState.addLayer(isAddLayer: (false, false, false, true), color: color, crude: 1, layout: (w: view.frame.width - 20, h: 44));
        
        lifePattern.topVertically(view: lifeState, predicate: "10")
        lifePattern.leadingAlign(view: view, predicate: "10")
        lifePattern.width(view: view, predicate: "*0.5")
        lifePattern.heightConstrain(predicate: "44")
        lifePattern.text = "生活模式"
        lifePattern.addLayer(isAddLayer: (false, false, false, true), color: color, crude: 1, layout: (w: view.frame.width - 20, h: 44));
        
        
        lifeValue.topVertically(view: lifeState, predicate: "10")
        lifeValue.leadingHorizontally(view: lifePattern, predicate: "0")
        lifeValue.width(view: view, predicate: "*0.5")
        lifeValue.heightConstrain(predicate: "44")
        //lifeValue.text = "在职"
        lifeValue.textAlignment = .center
        
        
        motionPattern.topVertically(view: lifePattern, predicate: "10")
        motionPattern.leadingAlign(view: view, predicate: "10")
        motionPattern.width(view: view, predicate: "*0.5")
        motionPattern.heightConstrain(predicate: "44")
        motionPattern.text = "运动模式"
        motionPattern.addLayer(isAddLayer: (false, false, false, true), color: color, crude: 1, layout: (w: view.frame.width - 20, h: 44))
        
        
        motionValue.topVertically(view: lifePattern, predicate: "10")
        motionValue.leadingHorizontally(view: motionPattern, predicate: "0")
        motionValue.width(view: view, predicate: "*0.5")
        motionValue.heightConstrain(predicate: "44")
        //motionValue.text = "从不"
        motionValue.textAlignment = .center
        
       
        diseaseTypes.topVertically(view: motionPattern, predicate: "15")
        diseaseTypes.leadingAlign(view: view, predicate: "10")
        diseaseTypes.width(view: view)
        diseaseTypes.heightConstrain(predicate: "44")
        diseaseTypes.text = "疾病类型 \n ddddddddddddddd"
        
        
        disease.topVertically(view: diseaseTypes, predicate: "2")
        disease.leadingAlign(view: view,predicate: "10")
        disease.width(view: view)
//        disease.heightConstrain(predicate: "44")
        disease.text = "该用户暂时未填写疾病类型"
        disease.addLayer(isAddLayer: (false, false, false, true), color: color, crude: 1, layout: (w: view.frame.width - 20, h: 44))
        
        describeCondition.topVertically(view: disease, predicate: "35")
        describeCondition.leadingAlign(view: view, predicate: "10")
        describeCondition.trailingAlign(view: view, predicate: "-10")
        describeCondition.heightConstrain(predicate: "44")
        describeCondition.text = "病情描述"
//        describeCondition.backgroundColor = .red
        describeCondition.addLayer(isAddLayer: (false, false, false, true), color: color, crude: 1, layout: (w: view.frame.width - 20, h: 44))
        
        describe.topVertically(view: describeCondition)
        describe.leadingAlign(view: view, predicate: "10")
        describe.trailingAlign(view: view, predicate: "-10")
        describe.heightConstrain(predicate: "44")
        describe.text = ""
    }
    
    // 设置数据
    func setJsonData(json: JSON) {
        
        self.title = json["nickname"].stringValue
        
        let _imgUrl = configure.shareConfigure.IP_diagonal + (json["iconUrl"].stringValue)
        headIcon.img(url: _imgUrl)
        
        
        name.text = json["nickname"].stringValue
        headDes.text = "\(json["age"].stringValue)岁      \(json["height"].stringValue)cm      \(json["weight"].stringValue)kg"
        
        
        // 计算属性
        var sportMode: String {
            // 运动情况
            if json["sportMode"] == JSON.null {
                return "从不"
            }else {
                return json["sportMode"].stringValue
            }
        }
        
        var liveMode: String {
           // 生活模式
            if json["liveMode"] == JSON.null {
                return "在职" //默认值
            }else {
                return json["liveMode"].stringValue
            }
        }
        
        
        lifeValue.text =  liveMode
        
        motionValue.text = sportMode
        
        describe.text = json["illnessDesc"].stringValue
    }
}
